//
//  BIZConsultDetailTableViewController.m
//  biz
//
//  Created by 边文辉 on 2017/3/17.
//  Copyright © 2017年 jieshuibao. All rights reserved.
//

#import "BIZConsultDetailTableViewController.h"
#import "BIZConsultQuestionCell.h"
#import "BIZConsultAnsCell.h"
#import "BIZConsultAnswererCell.h"
#import "BIZConsultQModel.h"
#import "BIZConsultAModel.h"
#import "BIZAddQuetionVC.h"
#import "BIAppraiseViewController.h"


@interface BIZConsultDetailTableViewController ()<UIPopoverPresentationControllerDelegate, BIZConsultAnsCellDelegate>

/** 是否显示追问 */
@property (nonatomic, assign) BOOL showPersistBtn;

/**  */
@property (nonatomic, strong) BIZConsultAModel *aModel;

@end

@implementation BIZConsultDetailTableViewController

static NSString *const QuesetionCellID = @"BIZConsultQuestionCellID";
static NSString *const AnsCellID = @"BIZConsultAnsCellID";
static NSString *const AnswererCellID = @"BIZConsultAnswererCellID";

- (BOOL)shouldAutorotate {
    return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"咨询详情";
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"评价" style:UIBarButtonItemStyleDone target:self action:@selector(rightBarButtonItemDidClicked:)];
    
    self.tableView.backgroundColor = HEXCOLOR(0xEEEDEE);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [UIView new];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([BIZConsultQuestionCell class]) bundle:nil] forCellReuseIdentifier:QuesetionCellID];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([BIZConsultAnsCell class]) bundle:nil] forCellReuseIdentifier:AnsCellID];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([BIZConsultAnswererCell class]) bundle:nil] forCellReuseIdentifier:AnswererCellID];
    
    self.tableView.estimatedRowHeight = 184;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    if (self.alertTitleStr.length > 0) {
        [UIAlertController showAlertViewWithTitle:self.alertTitleStr
                                          message:nil
                                cancelButtonTitle:@"关闭"
                               confirmButtonTitle:nil
                                      cancelBlock:nil
                                     confirmBlock:nil];
    }

    if (self.needLoadAnswer) {
        [self loadAnswer];
    }
}



- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (self.checkDetailBlock) {
        self.checkDetailBlock();
    }
}

- (void)loadAnswer
{
    [MBProgressHUD showAnimationHUD];
    [RequestTool POST:kConsultO2OAnswerUrl
           parameters:@{@"uuid" : _qModel.uuid}
             progress:nil
              success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//                  BLog(@"responseObject  %@", responseObject);
                  int code = [responseObject[@"code"] intValue];
                  if (code == 1) {
                      [MBProgressHUD hideHUD];
                      BIZConsultARetModel *rModel = [BIZConsultARetModel yy_modelWithJSON:responseObject];
                      _aModel = rModel.data;
                      [self.tableView reloadData];
                  } else if (code == 2) {
                      [MBProgressHUD showErrorMessage:responseObject[@"des"]];
                      
                      BIZLoginViewController *vc = [BIZLoginViewController viewControllerFromStoryboard];
                      MPWeakSelf(self);
                      vc.loginSuccessBlock = ^(){
                          [weakself loadAnswer];
                      };
                    
                  } else {
                      [MBProgressHUD showErrorMessage:responseObject[@"des"]];
                  }
                  
              }
              failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                  if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) {
                      [MBProgressHUD showErrorMessage:@"网络不给力"];
                  }
                  else {
                      [MBProgressHUD showErrorMessage:@"服务器繁忙"];
                  }

              }
     ];
}

- (void)rightBarButtonItemDidClicked:(UIBarButtonItem *)item
{
    NSString *title;
    
    if (!_aModel.stage) title = @"暂时没有答案可以评价哟-_-b";
    if (_aModel.score) title = @"您已评价过该问题";
    if (title) {
        [UIAlertController showAlertViewWithController:self
                                                 title:title
                                               message:nil
                                     cancelButtonTitle:nil
                                    confirmButtonTitle:@"确认"
                                           cancelBlock:nil
                                          confirmBlock:nil];
        
        return;
    }
    

    [self addChildViewController:[BIAppraiseViewController showWithURL:kConsultO2OAppraiseUrl uuid:_aModel.uuid finishBlock:^{
        [self.navigationController popViewControllerAnimated:YES];
    }]];
    
}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return _aModel.stage > 0 ? 2 : 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return section == 0 ? 1 : _aModel.stage + 1;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        BIZConsultQuestionCell *cell = [tableView dequeueReusableCellWithIdentifier:QuesetionCellID forIndexPath:indexPath];
        cell.model = _qModel;
        cell.isHistoryList = NO;
        cell.showImage = BIZConsultQuestionCellShowImageTypeShow;
        return cell;
    }
    
    if (indexPath.row == 0) {
        BIZConsultAnswererCell *cell = [tableView dequeueReusableCellWithIdentifier:AnswererCellID forIndexPath:indexPath];
        cell.model = self.aModel;
        return cell;
    }
    
    BIZConsultAnsCell *cell = [tableView dequeueReusableCellWithIdentifier:AnsCellID forIndexPath:indexPath];
    
    cell.index = indexPath.row;
    cell.model = self.aModel;
    cell.delegate = self;
    
    return cell;
}






- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}


#pragma mark - 选

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}


#pragma mark - 

- (void)BIZConsultAnsCellAskBtnDidClicked:(BIZConsultAnsCell *)cell
{
    MJWeakSelf;
    BIZAddQuetionVC *vc = [[BIZAddQuetionVC alloc]initWithUUID:self.aModel.uuid
                                                           url:kConsultO2OAskUrl
                                                    completion:^{
                                                        [weakSelf loadAnswer];
                                                    }];
    
    BIZNavigationController *nav = [[BIZNavigationController alloc] initWithRootViewController:vc];
    [self presentViewController:nav animated:YES completion:nil];
}

@end
